if(length(tryCatch(list.dirs(path="~/Library/CloudStorage/Dropbox/Robots and Trade in Services"),error=function(e) e))==0){
  base.path<-"~/Dropbox/Robots and Trade in Services/"
} else {
  base.path<-"~/Library/CloudStorage/Dropbox/Robots and Trade in Services/"
}

#Load data and functions
source(paste0(base.path,"Drafts/Replication/03 Functions and Processing.R"))

lucid.proc$policy_difference <- ifelse(lucid.proc$treatment_automation_labor=="Labor",lucid.proc$restrict_imports_difference,lucid.proc$restrict_automation_difference)

###########################
## Figure 6 (Appendix B) ##
###########################

library(sensemakr)

# Regress outcome on treatment and observables
m1.ctrl.ols <-lm(as.formula(paste("policy_difference~treatment_foreign_domestic_num+sample_date+", paste(balance_controls_vec, collapse=" + "))), data=subset(lucid.proc, (treatment_fdal == "Domestic Automation" | treatment_fdal == "Foreign Labor")))


# Regress treatment on observables
model.dx <- lm(as.formula(paste("treatment_foreign_domestic_num ~ +sample_date+", paste(balance_controls_vec, collapse=" + "))), data=subset(lucid.proc, (treatment_fdal == "Domestic Automation" | treatment_fdal == "Foreign Labor")))


# Calculate partial R2s
r2yx.d <- partial_r2(m1.ctrl.ols, covariates = "maleTRUE")
r2dx   <- partial_r2(model.dx, covariates = "maleTRUE")


# Formal bounds
formal_bound <- ovb_bounds(model = m1.ctrl.ols, 
                           treatment = "treatment_foreign_domestic_num", 
                           benchmark_covariates = c("maleTRUE"), 
                           kd = 1, ky = 1)                                                

ovb_contour_plot(m1.ctrl.ols,  
                 treatment = "treatment_foreign_domestic_num",
                 lim = .06)

# adds proper bound
add_bound_to_contour(bounds = formal_bound,
                     bound_label = c("Male"))


